package com.kj.xuyuan.adapter;

import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.kj.xuyuan.R;
import com.kj.xuyuan.http.UrlConstants;
import com.kj.xuyuan.model.RecommendAndNearBean;
import com.kj.xuyuan.util.GlideUtils;
import com.kj.xuyuan.util.StringUtils;

import java.util.List;

import androidx.annotation.Nullable;

public class RecommendAndNearAdapter extends BaseQuickAdapter<RecommendAndNearBean, BaseViewHolder> {

    private int type;//1推荐，不显示距离  2附近，显示距离

    public RecommendAndNearAdapter(int type) {
        super(R.layout.item_recommend);
        this.type = type;
    }

    @Override
    protected void convert(BaseViewHolder helper, RecommendAndNearBean item) {
        helper.setText(R.id.tv_name,item.getUser().getNickname());

        helper.setText(R.id.tv_sign,item.getUser().getBio());
        TextView tvSing = helper.getView(R.id.tv_sign);
        tvSing.setVisibility(StringUtils.isEmpty(item.getUser().getBio()) ? View.GONE : View.VISIBLE);


        helper.setText(R.id.tv_on_line_state,item.getRy_status() == 0 ? "在线" : "离线");
        helper.setImageResource(R.id.iv_on_line_state,item.getRy_status() == 0 ? R.mipmap.point_green : R.mipmap.point_gray);

        helper.setText(R.id.tv_like_or_chat,item.getIs_like() == 0 ? "喜欢" : "发消息");
        TextView tvLike = helper.getView(R.id.tv_like_or_chat);
        Drawable drawable = null;

        if(item.getIs_like() == 1){//点击过喜欢
            drawable = mContext.getResources().getDrawable(R.mipmap.icon_msg);
        }else {//未点击过喜欢
            drawable = mContext.getResources().getDrawable(R.mipmap.icon_like);
        }
        drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());
        tvLike.setCompoundDrawables(drawable,null,null,null);

        ImageView ivHead = helper.getView(R.id.iv_head);
        GlideUtils.loadCircleImage(mContext, UrlConstants.Base_Http+item.getUser().getAvatar(),ivHead);

        String tags = item.getUser().getTags();
        TextView tagOne = helper.getView(R.id.tv_tag_one);
        TextView tagTwo = helper.getView(R.id.tv_tag_two);
        TextView tagThree = helper.getView(R.id.tv_tag_three);
        if(StringUtils.isEmpty(tags)){
            tagOne.setVisibility(View.GONE);
            tagTwo.setVisibility(View.GONE);
            tagThree.setVisibility(View.GONE);
        }else{
            String[] tagList = item.getUser().getTags().split(",");
            if(tagList.length > 0){
                helper.setText(R.id.tv_tag_one,tagList[0]);
                tagOne.setVisibility(View.VISIBLE);
                if(tagList.length > 1){
                    helper.setText(R.id.tv_tag_two,tagList[1]);
                    tagTwo.setVisibility(View.VISIBLE);
                }else{
                    tagThree.setVisibility(View.GONE);
                }
                if(tagList.length > 2){
                    helper.setText(R.id.tv_tag_three,tagList[2]);
                    tagThree.setVisibility(View.VISIBLE);
                }
            }else{
                tagOne.setVisibility(View.GONE);
                tagTwo.setVisibility(View.GONE);
                tagThree.setVisibility(View.GONE);
            }
        }

        StringBuffer sb = new StringBuffer();
        if(!StringUtils.isEmpty(item.getYears())){
            sb.append(item.getYears()).append(" | ");
        }
        if(!StringUtils.isEmpty(item.getProvince())){
            sb.append(item.getProvince()).append(" | ");
        }
        if(!StringUtils.isEmpty(item.getHeight())){
            sb.append(item.getHeight()).append("cm").append(" | ");
        }
        if(!StringUtils.isEmpty(item.getIncome_start()) && !StringUtils.isEmpty(item.getIncome_end())){
            sb.append(item.getIncome_start()).append("-").append(item.getIncome_end()).append(" | ");
        }
        if(!StringUtils.isEmpty(item.getEducation())){
            sb.append(item.getEducation()).append(" | ");
        }
        if(sb.length() > 0){
            sb.deleteCharAt(sb.length()-1);
            helper.setText(R.id.tv_info,sb.toString());
            helper.setVisible(R.id.tv_info,true);
        }else{
            helper.setVisible(R.id.tv_info,false);
        }

        if(type == 1){
            helper.setVisible(R.id.tv_distance,false);
        }else{
            helper.setVisible(R.id.tv_distance,true);
            helper.setText(R.id.tv_distance,item.getDistance());
        }

        helper.addOnClickListener(R.id.tv_like_or_chat);

    }
}
